var a = 0;
var s = 0;

function register() {
	var yediv = document.getElementById("tabcontent1").style.display;
	var shangdiv = document.getElementById("tabcontent2").style.display;
	if (yediv == 'block' && shangdiv == 'none') {
		var username = $("#yeuser").val();
		if ("" == username) {
			alert("请填写用户名");
			$("#yeuser").focus();
			return;
		} else {
			checkloginuserName(username);
			if (a == 1 || a == 2) {
				return;
			}
		}
		if (a == 0) {
			var yepassword = $("#yepassword").val();
			if ("" == yepassword) {
				alert("请填写密码");
				$("#yepassword").focus();
				return;
			}
			var realname = $("#realname").val();
			if ("" == realname) {
				alert("请填写真实姓名");
				$("#realname").focus();
				return;
			}
			var yephone = $("#yephone").val();
			if ("" == yephone) {
				alert("请填写手机");
				$("#yephone").focus();
				return;
			}
			var sex = document.getElementById("sex").value;
			var tel = document.getElementById("tel").value;
			var yeaddress = $("#yeaddress").val();
			if ("" == yeaddress) {
				alert("请填写地址");
				$("#yeaddress").focus();
				return;
			}
			var yeagree = document.getElementById("yezhuagree");
			if (!yeagree.checked) {
				alert("请选择同意注册协议");
				return;
			}
			$.get(
					base + "user/register.shtml?user.username="
							+ encodeURI(encodeURI(username))
							+ "&user.password=" + yepassword + "&user.type=3"
							+ "&tgBrand.realName="
							+ encodeURI(encodeURI(realname)) + "&tgBrand.sex="
							+ sex + "&tgBrand.phone=" + yephone
							+ "&tgBrand.tel=" + tel + "&tgBrand.shopAddress="
							+ encodeURI(encodeURI(yeaddress)),
					function(message) {
						if (message == "1") {
							alert("注册成功");
							location.href = "../user/goForWord.shtml";

						} else if (message == "2") {
							alert("注册出错");
							return;
						} else if (message == "3") {
							alert("服务器出现异常，请稍后再试");
							return;
						}
					})
		}
	}
	if (yediv == 'none' && shangdiv == 'block') {
		var bigCategory = $("#bigCategory").val();
		if ("-1" == bigCategory) {
			alert("请选择类别");
			$("#bigCategory").focus();
			return;
		}
		var catogoId = $("#catogoId").val();
		if ("-1" == catogoId) {
			alert("请选择产品类型");
			$("#catogoId").focus();
			return;
		}
		var isSelect = false;
		var markstr = "";
		var str = "";
		var r = document.getElementsByName("typeId");

		for (var i = 0; i < r.length; i++) {
			if (r[i].checked) {
				isSelect = true;
				str += r[i].value;
				str += "_";
			}
		}
		if (!isSelect) {
			alert('请选择产品类型');
			return;
		}

		var ismarkSelect = false;
		var b = document.getElementsByName("markId");

		for (var i = 0; i < b.length; i++) {
			if (b[i].checked) {
				ismarkSelect = true;
				markstr += b[i].value;
				markstr += "_";
			}
		}
		if (!ismarkSelect) {
			alert('请选择所在商城');
			return;
		}
		var type = $("#type").val();
		if (type == "") {
			alert('请填写品牌');
			$("#type").focus();
			return;
		}
		var suser = $("#suser").val();
		if ("" == suser) {
			alert("请填写用户名");
			$("#suser").focus();
			return;
		} else {
			checklogshangjiainuserName(suser);
			if (s == 1 || s == 2)
				return;
			if (s == 0) {

				var spass = $("#spass").val();
				if ("" == spass) {
					alert("请填写密码");
					$("#spass").focus();
					return;
				}
				var scomanyName = $("#scomanyName").val();
				if ("" == scomanyName) {
					alert("请填写公司名字");
					$("#scomanyName").focus();
					return;
				}
				var saddress = $("#saddress").val();
				if ("" == saddress) {
					alert("请填写店铺地址");
					$("#saddress").focus();
					return;
				}
				var stel = $("#stel").val();
				if ("" == stel) {
					alert("请填写联系电话");
					$("#stel").focus();
					return;
				}
				var soper = $("#soper").val();
				if ("" == soper) {
					alert("请填写公司负责人");
					$("#soper").focus();
					return;
				}
				var shangjiaagree = document.getElementById("shangjiaagree");
				if (!shangjiaagree.checked) {
					alert("请选择同意注册协议");
					return;
				}
				$.get(base + "user/register.shtml?user.username="
						+ encodeURI(encodeURI(suser)) + "&user.password="
						+ spass + "&user.type=2" + "&tgBrand.companyName="
						+ encodeURI(encodeURI(scomanyName))
						+ "&tgBrand.shopAddress="
						+ encodeURI(encodeURI(saddress)) + "&tgBrand.phone="
						+ stel + "&tgBrand.companyOPerer="
						+ encodeURI(encodeURI(soper)) + "&tgBrand.name="
						+ encodeURI(encodeURI(type)) + "&tgBrand.categoryId="
						+ catogoId + "&typeStr=" + str + "&markStr=" + markstr,
						function(message) {
							if (message == "1") {
								alert("注册成功");
								location.href = "../user/goForWord.shtml";

							} else if (message == "2") {
								alert("注册出错");
								return;
							} else if (message == "3") {
								alert("服务器出现异常，请稍后再试");
								return;
							}
						})
			}
		}
	}

}

var checkUsername = false;
function checkloginuserName(username) {
	checkUsername = false;
	a = 0;
	var usernameLength = username.getBytes();
	var partern = /^[(\u4e00-\u9fa5)(a-z)(A-Z)(0-9)(_)]+$/i;
	if (!partern.test(username)) {
		a = 1;
		$("#successMes").hide();
		$("#errorMes").show();
		$("#errorMes").focus();
		$("#errorMes").html('用户名不合法，只能为汉字，数字，字母，下划线');
		return;
	}

	if (usernameLength < 6 || usernameLength > 18) {
		a = 1;
		$("#successMes").hide();
		$("#errorMes").show();
		$("#errorMes").focus();
		$("#errorMes").html('用户名为6到18位');
		return;
	}
	$("#errorMes").hide();
	$.ajax({
		url : base + 'user/checkUserName.shtml?username='
				+ encodeURI(username),
		method : "get",
		cache : false,
		success : function(message) {
			checkUsername = true;
			if (message == "2") {
				a = 1;
				$("#successMes").hide();
				$("#errorMes").show();
				$("#errorMes").focus();
				$("#errorMes").html('用户名已经被注册了');
				return;
			} else if (message == "3") {
				a = 2;
				$("#successMes").hide();
				$("#errorMes").show();
				$("#errorMes").focus();
				$("#errorMes").html('服务器出现异常，请稍后再试');
				return;
			} else if (message == "1") {
				a = 0;
				$("#errorMes").hide();
				$("#successMes").show();
				$("#successMes").html('该用户名可以使用');
			}
		}
	});
}

function checkPassword(password) {
	var usernameLength = password.getBytes();
	// var partern = /^[(a-z)(A-Z)(0-9)(_)(%)(~)(!)]+$/i;
	// if (!partern.test(password)) {
	// $("#successMes").hide();
	// $("#errorMes").show();
	// $("#errorMes").html('密码不合法，只能为数字，字母，下划线');
	// return;
	// }
	if (usernameLength < 6 || usernameLength > 18) {
		alert('密码为6到18位');
		$('#yepassword').focus();
		return;
	}
}

function checklogshangjiainuserName(username) {
	$.ajax({
		url : base + 'user/checkUserName.shtml?user.username='
				+ encodeURI(encodeURI(username)),
		method : "post",
		cache : false,
		success : function(message) {

			if (message == "2") {
				s = 1;
				alert("用户名已经被注册了");
				return;
			} else if (message == "3") {

				s = 2;
				alert("服务器出现异常，请稍后再试");
				return;
			}
		}
	});

}

function bigCategoryChange() {
	var bigCategoryValue = $("#bigCategory option:selected").val();
	if (bigCategoryValue != "") {

		$('#catogoId').html('加载中... ...');
		var str = '<option value="-1">--请选择--</option>';
		$.get(base + 'category/category_loadCategoryByPid.shtml?id='
				+ bigCategoryValue + '&t=' + new Date().getTime().toString(36),
				function(mes) {
					var json = eval('(' + mes + ')');
					for (var i = 0; i < json.length; i++) {
						str += '<option value="' + json[i]['id'] + '">'
								+ json[i]['name'] + '</option>';
					}
					$('#categoryId').html(str);
				})
	}
	loadshi(bigCategoryValue);
}
function smallCategoryChange() {
	var smallCategoryValue = $("#categoryId option:selected").val();
	if (smallCategoryValue != "") {
		$('#goodsType').html('加载中... ...');
		$.get(base + 'type/type_loadTypeByCategoryId.shtml?categoryId='
				+ smallCategoryValue + '&t='
				+ new Date().getTime().toString(36), function(mes) {
			var json = eval('(' + mes + ')');
			var str = ''
			for (var i = 0; i < json.length; i++) {
				if (i > 0 && i % 3 == 0)
					str += '<br/>';
				str += '<label><input type="checkbox" name="typeId"  value="'
						+ json[i]['id'] + '"/>' + json[i]['name']
						+ '</label>&nbsp;&nbsp;&nbsp;&nbsp;';
			}
			$('#goodsType').html(str);
		})
	}
}
function loadshi(bigCategoryId) {
	$.get(base + '/market/loadByCid.shtml?cid='+bigCategoryId+'&t='
			+ new Date().getTime().toString(36), function(mes) {
		var json = eval('(' + mes + ')');
		var str = ''
		for (var i = 0; i < json.length; i++) {
			if (i > 0 && i % 3 == 0)
				str += '<br/>';
			str += '<label><input type="checkbox" name="marketId"  value="'
					+ json[i]['id'] + '"/>' + json[i]['name']
					+ '</label>&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		$('#marktype').html(str);
	})
}

String.prototype.getBytes = function() {
	var cArr = this.match(/[^\x00-\xff]/ig);
	return this.length + (cArr == null ? 0 : cArr.length);
}

function checkUser() {
	var username = $("#yeuser").val();
	if ("" == username) {
		alert("请填写用户名");
		$("#yeuser").focus();
		return ;
	} else {
		if (!checkUsername) {
			a = 0;
			var usernameLength = username.getBytes();
			var partern = /^[(\u4e00-\u9fa5)(a-z)(A-Z)(0-9)(_)]+$/i;
			if (!partern.test(username)) {
				$("#successMes").hide();
				$("#errorMes").show();
				$("#errorMes").focus();
				$("#errorMes").html('用户名不合法，只能为汉字，数字，字母，下划线');
				return ;
			}

			if (usernameLength < 6 || usernameLength > 18) {
				$("#successMes").hide();
				$("#errorMes").show();
				$("#errorMes").focus();
				$("#errorMes").html('用户名为6到18位');
				return ;
			}
			$("#errorMes").hide();
			$.ajax({
				url : base + 'user/checkUserName.shtml?username='
						+ encodeURI(encodeURI(username)),
				method : "get",
				cache : false,
				success : function(message) {
					checkUsername = true;
					if (message == "2") {
						a = 1;
						$("#successMes").hide();
						$("#errorMes").show();
						$("#errorMes").focus();
						$("#errorMes").html('用户名已经被注册了');
						return ;
					} else if (message == "3") {
						a = 2;
						$("#successMes").hide();
						$("#errorMes").show();
						$("#errorMes").focus();
						$("#errorMes").html('服务器出现异常，请稍后再试');
						return ;
					} else if (message == "1") {
						a = 0;
						$("#errorMes").hide();
						$("#successMes").show();
						$("#successMes").html('该用户名可以使用');
						var yepassword = $("#yepassword").val();
						if ("" == yepassword) {
							alert("请填写密码");
							$("#yepassword").focus();
							return ;
						} else {
							var usernameLength = yepassword.getBytes();
							if (usernameLength < 6 || usernameLength > 18) {
								alert('密码为6到18位');
								$('#yepassword').focus();
								return;
							}
						}
						var realname = $("#realname").val();
						if ("" == realname) {
							alert("请填写真实姓名");
							$("#realname").focus();
							return ;
						}
						var yephone = $("#yephone").val();
						if ("" == yephone) {
							alert("请填写手机");
							$("#yephone").focus();
							return ;
						}
						var sex = document.getElementById("sex").value;
						var tel = document.getElementById("tel").value;
						var yeaddress = $("#yeaddress").val();
						if ("" == yeaddress) {
							alert("请填写地址");
							$("#yeaddress").focus();
							return ;
						}
						var yeagree = document.getElementById("yezhuagree");
						if (!yeagree.checked) {
							alert("请选择同意注册协议");
							return ;
						}
						document.forms[0].submit();
					}
				}
			});
		} else {
			if (a != 0) {
				alert('用户名非法，请重新输入!')
				$("#yeuser").focus();
				return ;
			}
			var yepassword = $("#yepassword").val();
			if ("" == yepassword) {
				alert("请填写密码");
				$("#yepassword").focus();
				return ;
			} else {
				var usernameLength = yepassword.getBytes();
				if (usernameLength < 6 || usernameLength > 18) {
					alert('密码为6到18位');
					$('#yepassword').focus();
					return ;
				}
			}
			var realname = $("#realname").val();
			if ("" == realname) {
				alert("请填写真实姓名");
				$("#realname").focus();
				return ;
			}
			var yephone = $("#yephone").val();
			if ("" == yephone) {
				alert("请填写手机");
				$("#yephone").focus();
				return ;
			}
			var sex = document.getElementById("sex").value;
			var tel = document.getElementById("tel").value;
			var yeaddress = $("#yeaddress").val();
			if ("" == yeaddress) {
				alert("请填写地址");
				$("#yeaddress").focus();
				return ;
			}
			var yeagree = document.getElementById("yezhuagree");
			if (!yeagree.checked) {
				alert("请选择同意注册协议");
				return ;
			}
			document.forms[0].submit();
		}
	}
}

function checkBrand() {
	var username = $("#suser").val();
	if ("" == username) {
		alert("请填写用户名");
		$("#suser").focus();
		return ;
	} else {
		if (!checkUsername) {
			a = 0;
			var usernameLength = username.getBytes();
			var partern = /^[(\u4e00-\u9fa5)(a-z)(A-Z)(0-9)(_)]+$/i;
			if (!partern.test(username)) {
				$("#successMes").hide();
				$("#errorMes").show();
				$("#errorMes").focus();
				$("#errorMes").html('用户名不合法，只能为汉字，数字，字母，下划线');
				return;
			}

			if (usernameLength < 6 || usernameLength > 18) {
				$("#successMes").hide();
				$("#errorMes").show();
				$("#errorMes").focus();
				$("#errorMes").html('用户名为6到18位');
				return;
			}
			$("#errorMes").hide();
			$.ajax({
				url : base + 'user/checkUserName.shtml?username='
						+ encodeURI(encodeURI(username)),
				method : "get",
				cache : false,
				success : function(message) {
					checkUsername = true;
					if (message == "2") {
						a = 1;
						$("#successMes").hide();
						$("#errorMes").show();
						$("#errorMes").focus();
						$("#errorMes").html('用户名已经被注册了');
						return false;
					} else if (message == "3") {
						a = 2;
						$("#successMes").hide();
						$("#errorMes").show();
						$("#errorMes").focus();
						$("#errorMes").html('服务器出现异常，请稍后再试');
						return false;
					} else if (message == "1") {
						a = 0;
						$("#errorMes").hide();
						$("#successMes").show();
						$("#successMes").html('该用户名可以使用');
						var yepassword = $("#spass").val();
						if ("" == yepassword) {
							alert("请填写密码");
							$("#spass").focus();
							return ;
						} else {
							var usernameLength = yepassword.getBytes();
							if (usernameLength < 6 || usernameLength > 18) {
								alert('密码为6到18位');
								$('#spass').focus();
								return;
							}
						}
						if (!checkBrandForm()) {
							return ;
						}
						document.forms[1].submit();
					}
				}
			});
		} else {
			if (a != 0) {
				alert('用户名非法，请重新输入!')
				$("#yeuser").focus();
				return ;
			}
			var yepassword = $("#spass").val();
			if ("" == yepassword) {
				alert("请填写密码");
				$("#spass").focus();
				return ;
			} else {
				var usernameLength = yepassword.getBytes();
				if (usernameLength < 6 || usernameLength > 18) {
					alert('密码为6到18位');
					$('#spass').focus();
					return ;
				}
				if (!checkBrandForm()) {
					return ;
				}
			}
			document.forms[1].submit();
		}
	}
}

function checkBrandForm() {
	var bigCategory = $("#bigCategory").val();
	if ("-1" == bigCategory) {
		alert("请选择所属行业");
		$("#bigCategory").focus();
		return false;
	}
	var catogoId = $("#categoryId").val();
	if ("-1" == catogoId) {
		alert("请选择产品分类");
		$("#categoryId").focus();
		return false;
	}
	var isSelect = false;
	var r = document.getElementsByName("typeId");

	for (var i = 0; i < r.length; i++) {
		if (r[i].checked) {
			isSelect = true;
		}
	}
	if (!isSelect) {
		alert('请选择产品类型');
		return false;
	}

	var ismarkSelect = false;
	var b = document.getElementsByName("marketId");

	for (var i = 0; i < b.length; i++) {
		if (b[i].checked) {
			ismarkSelect = true;
		}
	}
	if (!ismarkSelect) {
		alert('请选择所在商城');
		return false;
	}
	var type = $("#type").val();
	if (type == "") {
		alert('请填写品牌');
		$("#type").focus();
		return false;
	}
	var saddress = $("#saddress").val();
	if ("" == saddress) {
		alert("请填写店铺地址");
		$("#saddress").focus();
		return false;
	}
	var stel = $("#stel").val();
	if ("" == stel) {
		alert("请填写联系电话");
		$("#stel").focus();
		return false;
	}
	var soper = $("#soper").val();
	if ("" == soper) {
		alert("请填写公司负责人");
		$("#soper").focus();
		return false;
	}
	var shangjiaagree = document.getElementById("shangjiaagree");
	if (!shangjiaagree.checked) {
		alert("请选择同意注册协议");
		return false;
	}
	return true;
}
